#include "StringBad.h"
void callme1(BadString & aStr);
void callme2(BadString aStr);
using namespace std;

int main_string_bad()
{
    BadString bad1("Head line 1");
    BadString bad2("Head line 2");
    BadString bad3;
    // 仅仅到这一步时候的输出:
    /*
    C++ will dealloced ...
    Head line 2 will dealloced ...
    Head line 1 will dealloced ...
    */
    //-------------------
    /*
    加上后面两行代码之后的输出，从中可以看出，传入callme2的值传递先被释放，之后释放掉C++对象，之后再次释放Head line 2 时出现二次指针的情况
    这样就导致了二次释放，出现了crash
    String passed by reference ...
    Head line 1

    String passed by value ...
    Head line 2

    Head line 2 will dealloced ... 3 left
    C++ will dealloced ... 2 left
    P����� will dealloced ... 1 left
    outDebug(4213,0x11288d600) malloc: *** error for object 0x600002218050: pointer being freed was not allocated
    outDebug(4213,0x11288d600) malloc: *** set a breakpoint in malloc_error_break to debug
    */
    
    callme1(bad1);
    callme2(bad2);

    return 0;
}

void callme1(BadString & aStr) {
    cout << "String passed by reference ..." << endl;
    cout << aStr << endl;
}

void callme2(BadString aStr) {
    cout << "String passed by value ..." << endl;
    cout << aStr << endl;
}
